package sy3_design_principle.demeter;
/*
 * 两个实体类：CollegeEmployee和SchoolEmployee
 * 两个实体类对应的管理类：ColleageManager和SchoolManager
 * 在类SchoolManager方法 printAllEmployee()里，包含了陌生类CollegeEmployee，违反了迪米特原则
 * 违反了迪米特原则的后果是造成类SchoolManager扩展困难。
 *     例如，当修改CollegeEmployee结构时，需要相应地修改类SchoolManager。
 *     解决办法：将输出学院员工的代码放到类SchoolManager里
 * 
 * 局部变量：CollegeEmployee不是SchoolManager的直接朋友类
 * 方法参数类型：ColleageManager是SchoolManager的直接朋友类
 */
import java.util.ArrayList;
import java.util.List;

/*class CollegeEmployee{  //学院员工类
	private String id;

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}
class SchoolEmployee{  //学校总部员工类
	private String id;

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}
class ColleageManager{  //学院员工管理类
	public List<CollegeEmployee> getAllCollegeEmployee() {
		List<CollegeEmployee> list=new ArrayList<CollegeEmployee>();
		for(int i=0;i<5;i++) {
			CollegeEmployee collegeEmployee=new CollegeEmployee();
			collegeEmployee.setId("学院员工id="+i);
			list.add(collegeEmployee);
		}
		return list;
	}
}
class SchoolManager{  //学校员工管理类
	public List<SchoolEmployee> getAllSchoolEmployee() {
		List<SchoolEmployee> list=new ArrayList<SchoolEmployee>();
		for(int i=0;i<3;i++) {
			SchoolEmployee schoolEmployee=new SchoolEmployee();
			schoolEmployee.setId("学校总部员工id="+i);
			list.add(schoolEmployee);
		}
		return list;
	}
	
	//ColleageManager是SchoolManager的直接朋友类（方法参数类型）
	public void printAllEmployee(ColleageManager colleageManager) {  //方法有参数
		//下面的CollegeEmployee不是SchoolManager的直接朋友类---陌生类---局部变量
		List<CollegeEmployee> list1=colleageManager.getAllCollegeEmployee();
		System.out.println("-----学院员工-----");
		for(CollegeEmployee e:list1) {
			System.out.println(e.getId());
		}
		List<SchoolEmployee> list2=getAllSchoolEmployee();
		System.out.println("-----学校总部员工-----");
		for(SchoolEmployee e:list2) {
			System.out.println(e.getId());
		}
	}
}
public class Demeter {  //测试类
	public static void main(String[] args) {
		new SchoolManager().printAllEmployee(new ColleageManager());
	}
}*/

/*
 * 两个实体类：CollegeEmployee和SchoolEmployee
 * 两个实体类对应的管理类：ColleageManager和SchoolManager
 * 类SchoolManager和ColleageManager都不包含陌生类，符合迪米特原则
 */
class CollegeEmployee{  //学院员工类
	private String id;

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}

class SchoolEmployee{  //学校总部员工类
	private String id;

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}

class ColleageManager{  //学院员工管理类
	public List<CollegeEmployee> getAllCollegeEmployee() {
		List<CollegeEmployee> list=new ArrayList<CollegeEmployee>();
		for(int i=0;i<10;i++) {
			CollegeEmployee emp=new CollegeEmployee();
			emp.setId("学院员工id="+i);
			list.add(emp);
		}
		return list;
	}
	
	public void printEmployee() {  //方法无参数
		List<CollegeEmployee> list = getAllCollegeEmployee();  //不包含局部变量
		System.out.println("-----学院员工-----");
		for(CollegeEmployee e:list) {
			System.out.println(e.getId());
		}
	}
}

class SchoolManager{  //学校员工管理类
	public List<SchoolEmployee> getAllSchoolEmployee() {
		List<SchoolEmployee> list=new ArrayList<SchoolEmployee>();
		for(int i=0;i<5;i++) {
			SchoolEmployee schoolEmployee=new SchoolEmployee();
			schoolEmployee.setId("学校总部员工id="+i);
			list.add(schoolEmployee);
		}
		return list;
	}
	public void printAllEmployee(ColleageManager colleageManager) {  //方法参数为ColleageManager类型
		colleageManager.printEmployee(); //未暴露输出学院员工的实现代码（否则，违反迪米特法则！）
		
		List<SchoolEmployee> list2=getAllSchoolEmployee();
		System.out.println("-----学校总部员工-----");
		for(SchoolEmployee e:list2) {
			System.out.println(e.getId());
		}
	}
}

public class Demeter2 {  //主程序
	public static void main(String[] args) {
		new SchoolManager().printAllEmployee(new ColleageManager());
	}
}